package com.lagou.edu.homework.dao.impl;

import com.lagou.edu.homework.config.DataSourceConfig;
import com.lagou.edu.homework.dao.ResumeDAO;
import com.lagou.edu.homework.model.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: stage-3-module-2
 * @description:
 * @author: zhangxiaoyang
 * @date: 2020-08-26 00:53
 **/
@Repository
public class ResumeDAOImpl implements ResumeDAO {

    @Autowired
    private DataSourceConfig dataSourceConfig;


    @Override
    public Resume getById(Integer id) throws SQLException {
        Connection con = dataSourceConfig.getDataSource().getConnection();
        System.out.println("数据源信息：username："+con.getMetaData().getUserName()+",schema:"+con.getSchema());
        String sql = "select * from tb_resume where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();

        Resume resume = new Resume();
        while (resultSet.next()) {
            resume.setAddress(resultSet.getString("address"));
            resume.setName(resultSet.getString("name"));
            resume.setPhone(resultSet.getString("phone"));
            resume.setId(id.longValue());
        }

        resultSet.close();
        preparedStatement.close();
        con.close();

        return resume;
    }
}
